<?php
/**
 * Description: 下课回调接口
 * User: devforma
 * Date: 15/8/12
 * Time: 11:06
 */

class ClassOver implements iController {

    /**
     * api请求处理主函数
     * @param array $queryArgs 就是$_REQUEST变量
     * @return array api返回结果，必须含response_code
     */
    public function output($queryArgs) {
        global $cmsdb;

        if ($this->checkSign($queryArgs) && $queryArgs['success'] === 'true') {
            $cmsdb->update('data_live_class', array('over_time' => date('Y-m-d H:i:s', $queryArgs['endTime']/1000)),
                array('room_id' => $queryArgs['roomId']));

            return 'updated ' . $queryArgs['roomId'];
        }

        return 'fail';
    }

    /**
     * 校验参数有效性
     * @param array $params 请求参数
     * @return bool
     */
    private function checkSign($params) {
        $sign = $params['sign'];
        unset($params['sign']);

        ksort($params);

        $paramStr = '';
        foreach ($params as $k => $v) {
            $paramStr .= $k.'='.$v;
            $paramStr .= '&';
        }

        return $sign === md5(rtrim($paramStr, '&').'771f3c17303c49f6a8dc3e3ffbf41990');
    }
}